package org.dromara.vehicle.modules.carApply.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.vehicle.modules.carApply.domain.bo.CarApplyBo;
import org.dromara.vehicle.modules.carApply.domain.vo.ApplyVo;
import org.dromara.vehicle.modules.carApply.domain.vo.CarApplyVo;
import org.dromara.vehicle.modules.carApply.service.ICarApplyService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用车申请
 *
 * @author ckk
 * @date 2025-06-27
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/vehicle/carApply")
public class CarApplyController extends BaseController {

    private final ICarApplyService carApplyService;

    /**
     * 查询用车申请列表
     */
    @SaCheckPermission("vehicle:carApply:list")
    @GetMapping("/list")
    public TableDataInfo<CarApplyVo> list(CarApplyBo bo, PageQuery pageQuery) {
        return carApplyService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出用车申请列表
     */
    @SaCheckPermission("vehicle:carApply:export")
    @Log(title = "用车申请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(CarApplyBo bo, HttpServletResponse response) {
        List<CarApplyVo> list = carApplyService.queryList(bo);
        ExcelUtil.exportExcel(list, "用车申请", CarApplyVo.class, response);
    }

    /**
     * 获取用车申请详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("vehicle:carApply:query")
    @GetMapping("/{id}")
    public R<CarApplyVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        return R.ok(carApplyService.queryById(id));
    }

    /**
     * 新增用车申请
     */
    @SaCheckPermission("vehicle:carApply:add")
    @Log(title = "用车申请", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Long> add(@Validated(AddGroup.class) @RequestBody CarApplyBo bo) {
        return R.ok(carApplyService.insertByBo(bo));
    }

    /**
     * 修改用车申请
     */
    @SaCheckPermission("vehicle:carApply:edit")
    @Log(title = "用车申请", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Long> edit(@Validated(EditGroup.class) @RequestBody CarApplyBo bo) {
        return R.ok(carApplyService.updateByBo(bo));
    }

    /**
     * 删除用车申请
     *
     * @param ids 主键串
     */
    @SaCheckPermission("vehicle:carApply:remove")
    @Log(title = "用车申请", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(carApplyService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 合乘匹配度
     */
    @SaCheckPermission("vehicle:carApply:matchingDegree")
    @GetMapping("/matchingDegree/{id}")
    public R<List<ApplyVo>> matchingDegree(@PathVariable Long id) {
        List<ApplyVo> vos = carApplyService.matchingDegree(id);
        return R.ok(vos);
    }
}
